﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace BanVeXemPhim_Presentation
{
    public class GheDAO:DataProvider
    {
        public GheDTO[] LayDanhSachGheCuaRapChieu(int idrapchieu)
        {
            connect();
            string query = "select * from ghe where rapchieu=" + idrapchieu.ToString();
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            GheDTO[] gheDtos = new GheDTO[n];
            for (int i = 0; i < n; i++)
            {
                gheDtos[i] = (GheDTO)GetDataFromDataRow(dataTable, i);
            }
            return gheDtos;
        }


        public GheDTO[] LayDanhSachGheDangSuDungCuaRapChieu(int idrapchieu)
        {
            connect();
            string query = "select * from ghe where ghe.tinhtrang=1 and rapchieu=" + idrapchieu.ToString();
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            GheDTO[] gheDtos = new GheDTO[n];
            for (int i = 0; i < n; i++)
            {
                gheDtos[i] = (GheDTO)GetDataFromDataRow(dataTable, i);
            }
            return gheDtos;
        }

        protected override object GetDataFromDataRow(System.Data.DataTable dt, int i)
        {
            GheDTO ghe=new GheDTO();

            ghe.Hang = Convert.ToInt32(dt.Rows[i]["Hang"].ToString());
            ghe.Cot = Convert.ToInt32(dt.Rows[i]["Cot"].ToString());
            ghe.LoaiGhe = Convert.ToInt32(dt.Rows[i]["LoaiGhe"].ToString());
            ghe.TinhTrang = Convert.ToInt32(dt.Rows[i]["TinhTrang"].ToString());
            ghe.RapChieu = Convert.ToInt32(dt.Rows[i]["RapChieu"].ToString());
            ghe.IdGhe = Convert.ToInt32(dt.Rows[i]["IdGhe"].ToString());
            return ghe;
        }
        public GheDTO[] LayDanhSachGheDaDatTheoXuatChieu(XuatChieuDTO xuatChieu)
        {
            connect();
            string query = "select ghe.IdGhe,ghe.Hang,ghe.Cot,ghe.LoaiGhe,ghe.RapChieu,ghe.TinhTrang from ghe,ve where ghe.tinhtrang=1 and ve.ghe=ghe.idghe and ve.xuatchieu="+xuatChieu.IdXuatChieu.ToString();
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            GheDTO[] gheDtos = new GheDTO[n];
            for (int i = 0; i < n; i++)
            {
                gheDtos[i] = (GheDTO)GetDataFromDataRow(dataTable, i);
            }
            return gheDtos;
        }



        public void CapNhatDanhSachGhe(GheDTO[] DsGhe)
        {
            connect();
            int n = DsGhe.Length;
            for(int i=0;i<n;i++)
            {
                string query = "update ghe set tinhtrang="+DsGhe[i].TinhTrang+",loaighe="+DsGhe[i].LoaiGhe+" where idghe="+DsGhe[i].IdGhe;
                executeNonQuery(query);
            }
        }

        public void KhoiTaoDanhSachGheChoRap(RapChieuDTO rapchieuInfo)
        {
            connect();
            command = connection.CreateCommand();
            for(int i=0;i<rapchieuInfo.SoHang;i++)
            {
                for(int j=0;j<rapchieuInfo.SoCot;j++)
                {
                    string query = "insert into ghe(hang,cot,tinhtrang,loaighe,rapchieu) values("+i.ToString()+","+j.ToString()+",1,2,"+rapchieuInfo.IdRapChieu+")";
                    command.CommandText = query;
                    command.ExecuteNonQuery();
                }
            }
        }

        public void CapNhatTheoSoLuongHangCot(RapChieuDTO newrapChieuDto, int OldHang, int OldCot,int SoHangDaTao,int SoCotDaTao)
        {
            connect();
            command = connection.CreateCommand();
            int h;
            int c;
            if(SoCotDaTao*SoHangDaTao==0)
            {
                h = newrapChieuDto.SoHang;
                c = newrapChieuDto.SoCot;
                for(int i=0;i<h;i++)
                {
                    for(int j=0;j<c;j++)
                    {
                        string query = "insert into ghe(hang,cot,tinhtrang,loaighe,rapchieu) values(" + 
                            i.ToString() + "," + 
                            j.ToString() + "," + 
                            newrapChieuDto.TinhTrang.ToString() + 
                            ",2," + 
                            newrapChieuDto.IdRapChieu + ")";
                        command.CommandText = query;
                        command.ExecuteNonQuery();
                    }
                }
                return;
            }

            // Cập nhật, thêm.
            int NewHang = newrapChieuDto.SoHang;
            int NewCot = newrapChieuDto.SoCot;
            
            if(NewHang>SoHangDaTao)
            {
                for(int i=SoHangDaTao;i<NewHang;i++)
                {
                    for(int j=0;j<SoCotDaTao;j++)
                    {
                        string query = "insert into ghe(hang,cot,tinhtrang,loaighe,rapchieu) values(" + i.ToString() +
                                           "," + j.ToString() + ",0,2," + newrapChieuDto.IdRapChieu.ToString() + ")";
                        executeNonQuery(query);
                    }
                }
            }
            if(NewCot>SoCotDaTao)
            {
                for(int i=SoCotDaTao;i<NewCot;i++)
                {
                    int temp = (NewHang>SoHangDaTao)?NewHang:SoHangDaTao;
                    for (int j = 0; j < temp; j++)
                    {
                        string query = "insert into ghe(hang,cot,tinhtrang,loaighe,rapchieu) values(" + j.ToString() +
                                        "," + i.ToString() + ",0,2," + newrapChieuDto.IdRapChieu.ToString() + ")";
                        executeNonQuery(query);
                    }
                }
            }


            if (NewHang < OldHang)
            {
                for (int i = NewHang; i < SoHangDaTao; i++)
                {
                    string query = "update ghe set tinhtrang=0 where hang=" + i.ToString() + " and RapChieu = " + newrapChieuDto.IdRapChieu;
                    executeNonQuery(query);
                }
            }
            
            if(NewHang>OldHang)
            {
                for (int i = OldHang; i < NewHang; i++)
                {
                    string query = "update ghe set tinhtrang=1 where cot<" + NewCot + " and hang=" + i.ToString() + " and RapChieu = " + newrapChieuDto.IdRapChieu;
                    executeNonQuery(query);
                }
            }


            if (NewCot < OldCot)
            {
                for (int i = NewCot; i < SoCotDaTao; i++)
                {
                    string query = "update ghe set tinhtrang=0 where hang<"+NewHang+"  and cot=" + i.ToString() + " and RapChieu = " + newrapChieuDto.IdRapChieu;
                    executeNonQuery(query);
                }
            }
            if (NewCot > OldCot)
            {

                for (int i = OldCot; i < NewCot; i++)
                {
                    string query = "update ghe set tinhtrang=1 where hang<" + NewHang + "  and  cot=" + i.ToString() + " and RapChieu = " + newrapChieuDto.IdRapChieu;
                    executeNonQuery(query);
                }
            }

            //Cập nhât thông tin của rạp chiếu.
            string s = "update rapchieu set sohang=" + newrapChieuDto.SoHang + ",socot=" + newrapChieuDto.SoCot +
                           " where idrapchieu=" + newrapChieuDto.IdRapChieu.ToString();
            executeNonQuery(s);

        }
    }
}
